<template>
  <div class="login">
    <el-row class="login-row">
      <el-col :span="6">
        <el-card>
          <div class="tital">注册</div>
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
            <el-form-item prop="nickname">
              <el-input v-model="ruleForm.nickname" placeholder="请输入昵称"></el-input>
            </el-form-item>
            <el-form-item prop="email">
              <el-input v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
            </el-form-item>
            <div class="login-btn">
              <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            </div>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { register, checkNickname, checkEmail } from "../../api";
import { MessageBox } from "element-ui";

export default {
  data: function() {
    var checkNicknameVar = (rule, value, callback) => {
      checkNickname(value)
        .then(function(resp) {
          callback();
        })
        .catch(function(e) {
          callback(new Error("此昵称已存在"));
        });
    };
    var checkEmailVar = (rule, value, callback) => {
      checkEmail(value)
        .then(function(resp) {
          callback();
        })
        .catch(function(e) {
          callback(new Error("此邮箱注册过了"));
        });
    };
    return {
      ruleForm: {
        nickname: "",
        email: "",
        password: ""
      },
      rules: {
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          { validator: checkNicknameVar, trigger: ["blur", "change"] }
        ],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          },
          { validator: checkEmailVar, trigger: ["blur", "change"] }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      const self = this;
      self.$refs[formName].validate(valid => {
        if (valid) {
          register(
            self.ruleForm.nickname,
            self.ruleForm.email,
            self.ruleForm.password
          )
            .then(function(resp) {
              if (resp.errmsg === "ok") {
                MessageBox.alert("注册成功，请去注册邮箱激活账号", "注册成功", {
                  confirmButtonText: "确定",
                  callback: action => {
                    self.$router.push({ path: "/login" });
                  }
                });
              }
            })
            .catch(function(e) {
              console.log(e);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.tital {
  text-align: center;
  font-size: 26px;
  margin-bottom: 6%;
}

.login-btn button {
  width: 100%;
  height: 36px;
}

.login {
  display: flex;
  align-items: center;
  height: 100%;
}

.login-row {
  display: flex;
  width: 100%;
  flex-direction: column;
  align-items: center;
}
</style>